package com.example.pre.tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @ClassName: PagerTools.java
 * @Description: 内存分页工具类
 * 
 */
public class PagerTools<T> {

	/**
	 * 
	 * 内存分页
	 * 
	 * @param list      要分页的集合
	 * @param pageIndex 页下标
	 * @param pageSize  每页的大小
	 * @return list,page
	 */
	public Map<String, Object> getPage(List<T> list, int pageIndex, int pageSize) {
		Map<String, Object> data = new HashMap<String, Object>();
		int total = list.size();
		if (total == 0) {
			data.put("list", list);
			data.put("page", new Pager(pageIndex, pageSize, list.size()));
			return data;
		}
		int pageTotal = (int) Math.ceil((1.0 * total / pageSize));
		if (pageIndex < 1) {
			pageIndex = 1;
		}
		if (pageIndex > pageTotal) {
			pageIndex = pageTotal;
		}
		int startIndex = (pageIndex - 1) * pageSize;
		int endIndex = pageIndex * pageSize;
		if (endIndex > total) {
			endIndex = total;
		}
		data.put("list", list.subList(startIndex, endIndex));
		data.put("page", new Pager(pageIndex, pageSize, list.size()));
		return data;
	}

}
